<?php

namespace App\Http\Controllers\Admin;

use App\Goods;
use App\Http\Controllers\Controller;
use App\Http\Requests\GoodsAddRequest;
use App\Http\Response;
use App\Sku;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Uuid;

class GoodsController extends Controller
{
    use Response;
    public function index(Request $request)
    {
        $paginate=Goods::with("category")->when($request->get('keywords'),function ($query)use ($request){
            $query->where('name','like','%'.$request->get('keywords').'%')
                ->orWhere('summary','like','%'.$request->get('keywords').'%');
        })->when($request->get('keyword'),function ($query)use ($request){
            $query->where('name','like','%'.$request->get('keyword').'%')->orWhere('summary','like','%'.$request->get('keyword').'%');
        })->latest()->paginate($request->get('pageSize'),'*','pageNo');
        return $this->success(200,$paginate);
    }
    public function store(GoodsAddRequest $request)
    {
        $data=$request->except(["sku"]);
//        if (isset($data['pictures'])){
//            $data['pictures']=json_encode($data['pictures'],1);
//
//        }
        $create_goods=Goods::create($data);

            $sku=$request->get('sku');
            if ($sku){

                foreach ($sku as $key=>$value){
                    $ids=[];
                    $names=[];
                    foreach ($value['sku'] as $k=>$v){
                        array_push($ids,$v['spec_id'].':'.$v['id']);
                        array_push($names,$v['name']);
                    }
                    $data=[
                        'sku_code'=>Uuid::uuid1(),
                        'goods_id'=>$create_goods->id,
                        'spec'=>implode(",",$ids),
                        'spec_text'=>implode(",",$names),
                        'origin_price'=>$value['origin_price'],
                        'price'=>$value['price'],
                        'stock'=>$value['stock'],
                        'picture'=>isset($value['picture'])?$value['picture']:""
                    ];
                    Sku::create($data);
                }
            }
            DB::commit();
            return $this->message(200,'创建成功');


//        $prices=$request->get('price');
//        foreach ($prices as $key=>$v){
//            $data=[
//                'goods_pid'=>$pid,
//                'price'=>$v,
//                'currency'=>$key
//            ];
//            GoodsPrice::create($data);
//        }

    }
    public function show($id)
    {
        $goods=Goods::with(['skus'])->where('id',$id)->first();
//        $price=GoodsPrice::where('goods_pid',$pid)->get();

//        $goods_data['price']=$price;
        return $this->success(200,$goods);
    }
    public function update($id,Request $request)
    {
        $data = $request->except(array( "sku"));
        if (isset($data['pictures'])){
            $data['pictures']=json_encode($data['pictures']);
        }

        Goods::where('id',$id)->update($data);
        $sku = $request->get('sku');
        if ($sku) {
            Sku::where('goods_id', $id)->delete();
            foreach ($sku as $key => $value) {
                $ids = [];
                $names = [];
                foreach ($value['sku'] as $k => $v) {
                    array_push($ids, $v['spec_id'] . ':' . $v['id']);
                    array_push($names, $v['name']);
                }
                $data = [
                    'sku_code' => Uuid::uuid1(),
                    'goods_id' => $id,
                    'spec' => implode(",", $ids),
                    'spec_text' => implode(",", $names),
                    'origin_price' => $value['origin_price'],
                    'price' => $value['price'],
                    'stock'=>$value['stock'],
                    'picture'=>isset($value['picture'])?$value['picture']:""
                ];
                Sku::create($data);
            }

        }else{
            Sku::where('goods_id', $id)->delete();
        }
        return $this->message(200,'更新成功');
    }
    public function destroy($id)
    {
        $delete=Goods::where('id',$id)->delete();
        if ($delete){
           return $this->message(200, '删除成功');
        }else{
           return $this->message(200, '删除失败');
        }
    }
}
